package com.xoa.plugin;

import com.xoa.dao.flowhook.FlowHookMapper;
import com.xoa.dao.partyMember.PartyMemberMapper;
import com.xoa.global.proxy.TriggerSubject;
import com.xoa.model.common.SysCode;
import com.xoa.model.partyMember.PartyMemberWithBLOBs;
import com.xoa.model.sms.SmsBody;
import com.xoa.model.workflow.FlowHookWithBLOBs;
import com.xoa.model.workflow.TriggerPlugIn;
import com.xoa.service.sms.SmsService;
import com.xoa.util.FlowHtmlUtil;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 刘建 on 2020/7/24 14:15
 * 更新党建信息触发器
 */
@Service
public class PartyMemberTrigger implements TriggerSubject {

    @Autowired
    private FlowHookMapper flowHookMapper;

    @Resource
    PartyMemberMapper partyMemberMapper;

    @Autowired
    private SmsService smsService;

    @Override
    public void doRun(String company, Object... ags) {
        ContextHolder.setConsumerType(company);
        TriggerPlugIn tri = (TriggerPlugIn) ags[0];
        //判断是否是结束步骤
        boolean bol = false;
        if (tri != null) {
            if (tri.getFlowPrcs().equals("0")) {
                bol = true;
            } else {
                bol = false;
            }
            FlowHookWithBLOBs flowHookNew = new FlowHookWithBLOBs();
            flowHookNew.setHid(tri.getHookId());
            //获取对照关系
            FlowHookWithBLOBs flowHook = flowHookMapper.selectByhid(flowHookNew);
            Map<Object, Object> flowHookMaps = null;
            if (flowHook != null) {
                flowHookMaps = FlowHtmlUtil.queryTriggerField(tri.getModify(), tri.getReject(), flowHook.getMap(), flowHook.getDataDirection());
            }

            //这里为了给附件一个正确排序 定义了9对变量
            String attchId1 = "";
            String attachmentName1 = "";
            String attchId2 = "";
            String attachmentName2 = "";
            String attchId3 = "";
            String attachmentName3 = "";
            String attchId4 = "";
            String attachmentName4 = "";
            String attchId5 = "";
            String attachmentName5 = "";
            String attchId6 = "";
            String attachmentName6 = "";
            String attchId7 = "";
            String attachmentName7 = "";
            String attchId8 = "";
            String attachmentName8 = "";
            String attchId9 = "";
            String attachmentName9 = "";
            String reportTime = "";

            //获取表单内容
            if (flowHookMaps != null) {
                String selectPhase = "";
                String appr = "";
                String report = "";
                PartyMemberWithBLOBs partyMemberWithBLOBs = new PartyMemberWithBLOBs();
                for (Map.Entry<Object, Object> entry : flowHookMaps.entrySet()) {
                    //userId
                    if ("userId".equals(entry.getKey())) {

                    }
                    //审批意见
                    if ("APPROVAL".equals(entry.getKey())) {
                        appr = String.valueOf(entry.getValue());
                    }
                    //申请人出生日期
                    if ("BIRTHDAY".equals(entry.getKey())) {
                        String birthday = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        if (!"".equals(birthday)) {
                            try {
                                Date date = simpleDateFormat.parse(birthday);
                                partyMemberWithBLOBs.setBirthday(date);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    //民族
                    if ("FAMILY_NAME".equals(entry.getKey())) {
                        String familyName = (String) entry.getValue();
                        partyMemberWithBLOBs.setFamilyName(familyName);
                    }

                    //培养联系人姓名
                    if ("DEVELOP_CONTACTS".equals(entry.getKey())) {
                        String developContacts = (String) entry.getValue();
                        String[] split = developContacts.split("\\|");
                        String dc = "";
                        for (int i = 0; i < split.length; i += 2) {
                            dc += split[i];
                        }
                        partyMemberWithBLOBs.setDevelopContacts(dc);
                    }


                    //入党介绍人
                    if ("RECOMMEND_PERSON".equals(entry.getKey())) {
                        String rP = (String) entry.getValue();
                        String[] split = rP.split("\\|");
                        String rp2 = "";
                        for (int i = 0; i < split.length; i += 2) {
                            rp2 += split[i];
                        }
                        partyMemberWithBLOBs.setRecommendPerson(rp2);
                    }


                    //附件
                    if ("ATT_URL".equals(entry.getKey())) {
                        String attUrl = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        if (!"".equals(attUrl)) {
                            String[] split2 = attUrl.split("\\*");
                            String attchId = "";
                            String attchName = "";
                            for (int i = 0; i < split2.length; i++) {
                                String[] split = split2[i].split("&");
                                Map<String, String> hashMap = new HashMap();
                                for (String str : split) {
                                    String[] split1 = str.split("=");
                                    hashMap.put(String.valueOf(split1[0]), String.valueOf(split1[1]));
                                }
                                attchId += hashMap.get("AID") + "@" + hashMap.get("YM") + "_" + hashMap.get("ATTACHMENT_ID") + "*";
                                attchName += hashMap.get("ATTACHMENT_NAME") + "*";
                            }
                            partyMemberWithBLOBs.setAttachmentId(attchId);
                            partyMemberWithBLOBs.setAttachmentName(attchName);
                        }
                    }
                    if ("ATT_URL1".equals(entry.getKey())) {
                        String attUrl = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        if (!"".equals(attUrl)) {
                            String[] split2 = attUrl.split("\\*");
                            for (int i = 0; i < split2.length; i++) {
                                String[] split = split2[i].split("&");
                                Map<String, String> hashMap = new HashMap();
                                for (String str : split) {
                                    String[] split1 = str.split("=");
                                    hashMap.put(String.valueOf(split1[0]), String.valueOf(split1[1]));
                                }
                                attchId1 += hashMap.get("AID") + "@" + hashMap.get("YM") + "_" + hashMap.get("ATTACHMENT_ID") + "*";
                                attachmentName1 += hashMap.get("ATTACHMENT_NAME") + "*";
                            }

                        }
                    }
                    if ("ATT_URL2".equals(entry.getKey())) {
                        String attUrl = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        if (!"".equals(attUrl)) {
                            String[] split2 = attUrl.split("\\*");
                            for (int i = 0; i < split2.length; i++) {
                                String[] split = split2[i].split("&");
                                Map<String, String> hashMap = new HashMap();
                                for (String str : split) {
                                    String[] split1 = str.split("=");
                                    hashMap.put(String.valueOf(split1[0]), String.valueOf(split1[1]));
                                }
                                attchId2 += hashMap.get("AID") + "@" + hashMap.get("YM") + "_" + hashMap.get("ATTACHMENT_ID") + "*";
                                attachmentName2 += hashMap.get("ATTACHMENT_NAME") + "*";
                            }

                        }

                    }
                    if ("ATT_URL3".equals(entry.getKey())) {
                        String attUrl = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        if (!"".equals(attUrl)) {
                            String[] split2 = attUrl.split("\\*");
                            for (int i = 0; i < split2.length; i++) {
                                String[] split = split2[0].split("&");
                                Map<String, String> hashMap = new HashMap();
                                for (String str : split) {
                                    String[] split1 = str.split("=");
                                    hashMap.put(String.valueOf(split1[0]), String.valueOf(split1[1]));
                                }
                                attchId3 += hashMap.get("AID") + "@" + hashMap.get("YM") + "_" + hashMap.get("ATTACHMENT_ID") + "*";
                                attachmentName3 += hashMap.get("ATTACHMENT_NAME") + "*";
                            }

                        }
                    }
                    if ("ATT_URL4".equals(entry.getKey())) {
                        String attUrl = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        if (!"".equals(attUrl)) {
                            String[] split2 = attUrl.split("\\*");
                            for (int i = 0; i < split2.length; i++) {
                                String[] split = split2[0].split("&");
                                Map<String, String> hashMap = new HashMap();
                                for (String str : split) {
                                    String[] split1 = str.split("=");
                                    hashMap.put(String.valueOf(split1[0]), String.valueOf(split1[1]));
                                }
                                attchId4 += hashMap.get("AID") + "@" + hashMap.get("YM") + "_" + hashMap.get("ATTACHMENT_ID") + "*";
                                attachmentName4 += hashMap.get("ATTACHMENT_NAME") + "*";
                            }

                        }
                    }
                    if ("ATT_URL5".equals(entry.getKey())) {
                        String attUrl = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        if (!"".equals(attUrl)) {
                            String[] split2 = attUrl.split("\\*");
                            for (int i = 0; i < split2.length; i++) {
                                String[] split = split2[0].split("&");
                                Map<String, String> hashMap = new HashMap();
                                for (String str : split) {
                                    String[] split1 = str.split("=");
                                    hashMap.put(String.valueOf(split1[0]), String.valueOf(split1[1]));
                                }
                                attchId5 += hashMap.get("AID") + "@" + hashMap.get("YM") + "_" + hashMap.get("ATTACHMENT_ID") + "*";
                                attachmentName5 += hashMap.get("ATTACHMENT_NAME") + "*";
                            }

                        }

                    }
                    if ("ATT_URL6".equals(entry.getKey())) {
                        String attUrl = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        if (!"".equals(attUrl)) {
                            String[] split2 = attUrl.split("\\*");
                            for (int i = 0; i < split2.length; i++) {
                                String[] split = split2[0].split("&");
                                Map<String, String> hashMap = new HashMap();
                                for (String str : split) {
                                    String[] split1 = str.split("=");
                                    hashMap.put(String.valueOf(split1[0]), String.valueOf(split1[1]));
                                }
                                attchId6 += hashMap.get("AID") + "@" + hashMap.get("YM") + "_" + hashMap.get("ATTACHMENT_ID") + "*";
                                attachmentName6 += hashMap.get("ATTACHMENT_NAME") + "*";
                            }

                        }

                    }
                    if ("ATT_URL7".equals(entry.getKey())) {
                        String attUrl = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        if (!"".equals(attUrl)) {
                            String[] split2 = attUrl.split("\\*");
                            for (int i = 0; i < split2.length; i++) {
                                String[] split = split2[0].split("&");
                                Map<String, String> hashMap = new HashMap();
                                for (String str : split) {
                                    String[] split1 = str.split("=");
                                    hashMap.put(String.valueOf(split1[0]), String.valueOf(split1[1]));
                                }
                                attchId7 += hashMap.get("AID") + "@" + hashMap.get("YM") + "_" + hashMap.get("ATTACHMENT_ID") + "*";
                                attachmentName7 += hashMap.get("ATTACHMENT_NAME") + "*";
                            }

                        }

                    }
                    if ("ATT_URL8".equals(entry.getKey())) {
                        String attUrl = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        if (!"".equals(attUrl)) {
                            String[] split2 = attUrl.split("\\*");
                            for (int i = 0; i < split2.length; i++) {
                                String[] split = split2[0].split("&");
                                Map<String, String> hashMap = new HashMap();
                                for (String str : split) {
                                    String[] split1 = str.split("=");
                                    hashMap.put(String.valueOf(split1[0]), String.valueOf(split1[1]));
                                }
                                attchId8 += hashMap.get("AID") + "@" + hashMap.get("YM") + "_" + hashMap.get("ATTACHMENT_ID") + "*";
                                attachmentName8 += hashMap.get("ATTACHMENT_NAME") + "*";
                            }

                        }

                    }
                    if ("ATT_URL9".equals(entry.getKey())) {
                        String attUrl = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        if (!"".equals(attUrl)) {
                            String[] split2 = attUrl.split("\\*");
                            for (int i = 0; i < split2.length; i++) {
                                String[] split = split2[0].split("&");
                                Map<String, String> hashMap = new HashMap();
                                for (String str : split) {
                                    String[] split1 = str.split("=");
                                    hashMap.put(String.valueOf(split1[0]), String.valueOf(split1[1]));
                                }
                                attchId9 += hashMap.get("AID") + "@" + hashMap.get("YM") + "_" + hashMap.get("ATTACHMENT_ID") + "*";
                                attachmentName9 += hashMap.get("ATTACHMENT_NAME") + "*";
                            }

                        }

                    }
                    //审核通过时间
                    if ("STATE_TIME".equals(entry.getKey())) {
                        String stateTime = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        if (!"".equals(stateTime)) {
                            try {
                                Date date = simpleDateFormat.parse(stateTime);
                                partyMemberWithBLOBs.setStateTime(date);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    //入党通过申请时间
                    if ("APPLY_TIME".equals(entry.getKey())) {
                        String applyTime = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        if (!"".equals(applyTime)) {
                            try {
                                Date date = simpleDateFormat.parse(applyTime);
                                partyMemberWithBLOBs.setApplyTime(date);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    //确定积极分子时间ACTIVISTS_TIME
                    if ("ACTIVISTS_TIME".equals(entry.getKey())) {
                        String activistsTime = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        if (!"".equals(activistsTime)) {
                            try {
                                Date date = simpleDateFormat.parse(activistsTime);
                                partyMemberWithBLOBs.setActivistsTime(date);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    //确定发展对象时间DEVELOP_TIME
                    if ("DEVELOP_TIME".equals(entry.getKey())) {
                        String developTime = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

                        try {
                            if (!"".equals(developTime)) {
                                Date date = simpleDateFormat.parse(developTime);
                                partyMemberWithBLOBs.setDevelopTime(date);
                            } else {
                                partyMemberWithBLOBs.setDevelopTime(new Date());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                    //确定预备党员时间PREPARE_TIME
                    if ("PREPARE_TIME".equals(entry.getKey())) {
                        String prepareTime = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        try {
                            if (!"".equals(prepareTime)) {
                                Date date = simpleDateFormat.parse(prepareTime);
                                partyMemberWithBLOBs.setPrepareTime(date);
                            } else {
                                partyMemberWithBLOBs.setPrepareTime(new Date());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                    //预备党员接收时间RECEIVE_TIME
                    if ("RECEIVE_TIME".equals(entry.getKey())) {
                        String receiveTimme = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        try {
                            if (!"".equals(receiveTimme)) {
                                Date date = simpleDateFormat.parse(receiveTimme);
                                partyMemberWithBLOBs.setReceiveTime(date);
                            } else {
                                partyMemberWithBLOBs.setPrepareTime(new Date());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    if ("REPORT_TIME".equals(entry.getKey())) {
                        String rT = entry.getValue() != null ? String.valueOf(entry.getValue()) : "";
                        reportTime += rT + ",";
                    }
                    //这2个只在选择发展阶段流程中使用
                    if ("PHASE".equals(entry.getKey())) {
                        String phase = String.valueOf(entry.getValue());
                        partyMemberWithBLOBs.setDevelopPhase(phase);
                    }
                    //标识进入申请入党选择发展阶段流程
                    if ("SELECT_PHASE".equals(entry.getKey())) {
                        selectPhase = "1";
                    }

                    //培养联系人变更
                    if ("CONTACTS_CHANGE".equals(entry.getKey())) {
                        selectPhase = "6";
                    }

                    //入党介绍人变更
                    if ("PARTY_INTRODUCER".equals(entry.getKey())) {
                        selectPhase = "8";
                    }
                    //思想汇报
                    if ("REPORT".equals(entry.getKey())) {
                        selectPhase = "9";
                    }

                }

                String attchIdAll = attchId1 + "," + attchId2 + "," + attchId3 + "," + attchId4 + "," + attchId5 + "," +
                        attchId6 + "," + attchId7 + "," + attchId8 + "," + attchId9;
                String attchmentNameAll = attachmentName1 + "," + attachmentName2 + "," + attachmentName3 + "," + attachmentName4 + "," +
                        attachmentName5 + "," + attachmentName6 + "," + attachmentName7 + "," + attachmentName8 + "," + attachmentName9;

                PartyMemberWithBLOBs partyMember = partyMemberMapper.findPartyMemberByRunId(Integer.parseInt(tri.getRunId()));
                //审批不通过,直接返回
                if ("不同意".equals(appr)) {
                    if (partyMember.getDevelopPhase().equals("1")) {//如果是1的阶段,则退回0阶段
                        partyMember.setDevelopPhase("0");

                    }
                    if (partyMember.getDevelopPhase().equals("3") &&
                            (partyMember.getNodeStatus().equals("1") || partyMember.getNodeStatus().equals("0"))) {
                        partyMember.setNodeStatus("0");
                    }
                    if (partyMember.getDevelopPhase().equals("5") && partyMember.getNodeStatus().equals("5")) {
                        partyMember.setNodeStatus("4");
                    }
                    partyMember.setState("2");
                    partyMemberMapper.updateByPrimaryKeySelective(partyMember);
                    return;
                }
                //申请入党选择发展阶段流程
                if ("1".equals(selectPhase)) {
                    //说明这个是入党选择发展阶段的流程
                    //直接更新partyMember表单信息即可
                    partyMemberWithBLOBs.setNodeStatus("0");
                    partyMemberWithBLOBs.setState("2");
                    if (partyMember == null) {

                        partyMemberMapper.insertSelective(partyMemberWithBLOBs);
                    } else {
                        partyMember.setNodeStatus("0");
                        partyMember.setState("2");
                        partyMember.setDevelopPhase(partyMemberWithBLOBs.getDevelopPhase());
                        partyMemberMapper.updateByPrimaryKeySelective(partyMember);
                    }
                    return;
                }

                //培养联系人变更阶段
                if ("6".equals(selectPhase)) {
                    //培养联系人变更表附件
                    partyMember.setAttachmentNameRecommend(attachmentName1);
                    partyMember.setAttachmentIdeRecommend(attchId1);
                    partyMember.setDevelopContacts(partyMemberWithBLOBs.getDevelopContacts());
                    partyMemberMapper.updateByPrimaryKeySelective(partyMember);
                    return;
                }

                //入党介绍人变更阶段
                if ("8".equals(selectPhase)) {
                    //培养联系人变更表附件
                    partyMember.setAttachmentNameDevelops(attachmentName1);
                    partyMember.setAttachmentIdDevelops(attchId1);
                    partyMember.setRecommendPerson(partyMemberWithBLOBs.getRecommendPerson());
                    partyMemberMapper.updateByPrimaryKeySelective(partyMember);
                    return;
                }
                if ("9".equals(selectPhase)) {
                    //预备党员的
                    if (partyMember.getDevelopPhase().equals("5")) {
                        if (partyMember.getAttachmentIdThoughtReport() == null || "".equals(partyMember.getAttachmentIdThoughtReport())) {
                            partyMember.setAttachmentIdThoughtReport(attchId1+"*");
                            partyMember.setAttachmentNameThoughtReport(attachmentName1+"*");
                            partyMember.setThoughtReportTime(reportTime);
                        } else {
                            partyMember.setAttachmentIdThoughtReport(partyMember.getAttachmentIdThoughtReport() + "," + attchId1+"*");
                            partyMember.setAttachmentNameThoughtReport(partyMember.getAttachmentNameThoughtReport() + "," + attachmentName1+"*");
                            partyMember.setThoughtReportTime(partyMember.getThoughtReportTime() + reportTime);
                        }
                    } else if (partyMember.getDevelopPhase().equals("4") ||
                            (partyMember.getDevelopPhase().equals("3") &&
                                    partyMember.getNodeStatus().equals("3"))) {//发展对象
                        if (partyMember.getAttachmentIdDevelopsReport() == null || "".equals(partyMember.getAttachmentIdDevelopsReport())) {
                            partyMember.setAttachmentIdDevelopsReport(attchId1);
                            partyMember.setAttachmentNameDevelopsReport(attachmentName1);
                            partyMember.setDevelopsReportTime(reportTime);
                        } else {
                            partyMember.setAttachmentIdDevelopsReport(partyMember.getAttachmentIdReport() + "," + attchId1+"*");
                            partyMember.setAttachmentNameDevelopsReport(partyMember.getAttachmentNameReport() + "," + attachmentName1+"*");
                            partyMember.setDevelopsReportTime(partyMember.getDevelopsReportTime() + reportTime);
                        }
                    } else if (partyMember.getDevelopPhase().equals("2") ||
                            (partyMember.getDevelopPhase().equals("3") &&
                                    partyMember.getNodeStatus().equals("1"))) {//积极分子
                        if (partyMember.getAttachmentIdReport() == null || "".equals(partyMember.getAttachmentIdReport())) {
                            partyMember.setAttachmentIdReport(attchId1);
                            partyMember.setAttachmentNameReport(attachmentName1);
                            partyMember.setReportTime(reportTime);
                        } else {
                            partyMember.setAttachmentIdReport(partyMember.getAttachmentIdReport() + "," + attchId1+"*");
                            partyMember.setAttachmentNameReport(partyMember.getAttachmentNameReport() + "," + attachmentName1+"*");
                            partyMember.setReportTime(partyMember.getReportTime() + reportTime);
                        }
                    }
                    partyMemberMapper.updateByPrimaryKeySelective(partyMember);
                    //=========================提醒========================
                    try{
                        smsService.updatequerySmsByType("82",partyMember.getUserId(),"/partyMember/partyTR");
                        SmsBody smsBody = new SmsBody();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = sdf.parse(reportTime.split(",")[0]);
                        smsBody.setFromId("admin");
                        smsBody.setContent("请注意提交思想汇报");
                        smsBody.setSendTime((int) (date.getTime() / 1000)+75*24*60*60);
                        smsBody.setSmsType("82");
                        smsBody.setRemindUrl("/partyMember/partyTR");
                        String sqlType =tri.getSqlType();
                        //String sqlType = (String) request.getSession().getAttribute("loginDateSouse");
                        smsService.saveSms(smsBody, partyMember.getUserId(), "1", "2", "请注意提交思想汇报！", "请注意提交思想汇报", sqlType);
                    }catch (Exception e){

                    }

                    return;
                }


                //字段developPhase和node_status说明
                //developPhase判断的是主阶段,0代表申请入党,2代表积极分子,3代表发展对象,4代表预备党员(未接收),5代表预备党员(接收),6代表正式党员(此阶段不会出现在触发器内)
                //node_status判断的是子节点,例如:
                //developPhase=3,node_status=1,是发展对象阶段,第一步骤
                //如果node_status=0,说明是从申请入党选择的发展阶段,而不是从developPhase=0开始的,这时候他代表的也是该主阶段的第一步骤,但是没有此步骤的条件判断(在申请提交阶段)
                //node_status = 0和node_status=1同为第一步骤,有哪个都行
                //0,申请入党,1入党审核阶段没有node_status的要求,因为他们只有1个步骤;
                switch (partyMember.getDevelopPhase()) {
                    case "0"://申请阶段
                        //申请人
                        if (partyMemberWithBLOBs.getBirthday() != null) {
                            partyMember.setBirthday(partyMemberWithBLOBs.getBirthday());//生日
                        }
                        partyMember.setFamilyName(partyMemberWithBLOBs.getFamilyName());
                        //入党申请书附件
                        partyMember.setAttachmentIdApply(partyMemberWithBLOBs.getAttachmentId());
                        partyMember.setAttachmentNameApply(partyMemberWithBLOBs.getAttachmentName());
                        //提交状态
                        partyMember.setDevelopPhase("1");
                        partyMember.setState("0");
                        if (partyMember.getOldRunId() == null) {
                            partyMember.setOldRunId(String.valueOf(partyMember.getRunId()));
                        } else {
                            partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                        }
                        break;
                    case "1"://申请通过阶段
                        //谈话记录附件
                        partyMember.setAttachmentIdApply(partyMember.getAttachmentIdApply() + "," + attchId1);
                        partyMember.setAttachmentNameApply(partyMember.getAttachmentNameApply() + "," + attachmentName1);
                        //记录申请通过时间
                        partyMember.setApplyTime(partyMemberWithBLOBs.getApplyTime());
                        //提交状态到积极分子
                        String attachmentIdApply = partyMember.getAttachmentIdApply();
                        String[] split = attachmentIdApply.split(",");
                        if (split.length == 2) {
                            partyMember.setDevelopPhase("2");
                        }
                        partyMember.setState("2");
                        partyMember.setNodeStatus("1");
                        partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                        break;
                    case "2"://入党积极分子
                        //附件
                        //判断node_status是否是0或者1
                        if (partyMember.getNodeStatus().equals("0") || partyMember.getNodeStatus().equals("1")) {
                            //确认积极分子时间
                            partyMember.setActivistsTime(partyMemberWithBLOBs.getActivistsTime());
                            partyMember.setAttachmentIdPositive(attchId1 + "," + attchId2 + "," + attchId3 + "," + attchId4 + "," + attchId5 + "," + attchId6 + "," + attchId7 + "," + attchId8);
                            partyMember.setAttachmentNamePositive(attachmentName1 + "," + attachmentName2 + "," + attachmentName3 + "," + attachmentName4 + "," + attachmentName5 + "," + attachmentName6 + "," + attachmentName7 + "," + attachmentName8);
                            partyMember.setDevelopContacts(partyMemberWithBLOBs.getDevelopContacts());
                            partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                            partyMember.setState("2");
                            partyMember.setNodeStatus("2");
                            smsService.updatequerySmsByType("82",partyMember.getUserId(),"/partyMember/partyTR");
                            SmsBody smsBody = new SmsBody();
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            Date date = new Date();
                            smsBody.setFromId("admin");
                            smsBody.setContent("请提交思想汇报");
                            smsBody.setSendTime((int) (date.getTime() / 1000));
                            smsBody.setSmsType("82");
                            smsBody.setRemindUrl("/partyMember/partyTR");
                            String sqlType =tri.getSqlType();
                            //String sqlType = (String) request.getSession().getAttribute("loginDateSouse");
                            smsService.saveSms(smsBody, partyMember.getUserId(), "1", "2", "请提交思想汇报！", "请提交思想汇报", sqlType);
                            break;
                        } else {//已经提交过申请积极分子,需要提交4次思想汇报
                            /*if (partyMember.getNodeStatus().equals("2")) {//第一次提交思想汇报
                                partyMember.setAttachmentIdPositive(partyMember.getAttachmentIdPositive() + "," + partyMemberWithBLOBs.getAttachmentId());
                                partyMember.setAttachmentNamePositive(partyMember.getAttachmentNamePositive() + "," + partyMemberWithBLOBs.getAttachmentName());
                                partyMember.setReports("1");
                                partyMember.setNodeStatus("3");
                                partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                                partyMember.setState("2");
                                break;
                            }
                            if (partyMember.getNodeStatus().equals("3")) {//第二次提交思想汇报
                                partyMember.setAttachmentIdPositive(partyMember.getAttachmentIdPositive() + "," + partyMemberWithBLOBs.getAttachmentId());
                                partyMember.setAttachmentNamePositive(partyMember.getAttachmentNamePositive() + "," + partyMemberWithBLOBs.getAttachmentName());
                                partyMember.setReports("2");
                                partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                                partyMember.setState("2");
                                partyMember.setNodeStatus("4");
                                break;
                            }
                            if (partyMember.getNodeStatus().equals("4")) {//第三次提交思想汇报
                                partyMember.setAttachmentIdPositive(partyMember.getAttachmentIdPositive() + "," + partyMemberWithBLOBs.getAttachmentId());
                                partyMember.setAttachmentNamePositive(partyMember.getAttachmentNamePositive() + "," + partyMemberWithBLOBs.getAttachmentName());
                                partyMember.setReports("3");
                                partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                                partyMember.setState("2");
                                partyMember.setNodeStatus("5");
                                break;
                            }
                            if (partyMember.getNodeStatus().equals("5")) {//第四次提交思想汇报后转变状态到发展对象
                                partyMember.setReports("4");//4是给预备党员思想汇报做初始准备
                                partyMember.setAttachmentIdPositive(partyMember.getAttachmentIdPositive() + "," + partyMemberWithBLOBs.getAttachmentId());
                                partyMember.setAttachmentNamePositive(partyMember.getAttachmentNamePositive() + "," + partyMemberWithBLOBs.getAttachmentName());
                                partyMember.setNodeStatus("6");
                                break;
                            }*/
                            //跳过了思想汇报,直接进入提交考察信息
                            if (partyMember.getNodeStatus().equals("2")) {
                                partyMember.setAttachmentIdPositive(partyMember.getAttachmentIdPositive() + "," + attchId1);
                                partyMember.setAttachmentNamePositive(partyMember.getAttachmentNamePositive() + "," + attachmentName1);
                                partyMember.setState("2");
                                partyMember.setNodeStatus("1");
                                //记录审核通过时间
                                //提交下一阶段
                                partyMember.setDevelopPhase("3");
                                partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                                break;
                            }
                        }
                        break;
                    case "3"://发展对象
                        //确定发展对象
                        if (partyMember.getNodeStatus().equals("0") || partyMember.getNodeStatus().equals("1")) {

                            partyMember.setAttachmentIdDevelop(attchId1 + "," + attchId2 + "," + attchId3 + "," + attchId4 + "," + attchId5 + "," + partyMemberWithBLOBs.getAttachmentId());
                            partyMember.setAttachmentNameDevelop(attachmentName1 + "," + attachmentName2 + "," + attachmentName3 + "," + attachmentName4 + "," + attachmentName5 + "," + partyMemberWithBLOBs.getAttachmentName());
                            partyMember.setRecommendPerson(partyMemberWithBLOBs.getRecommendPerson());
                            partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                            partyMember.setDevelopTime(partyMemberWithBLOBs.getDevelopTime());//确定发展对象时间
                            partyMember.setState("2");
                            partyMember.setNodeStatus("3");
                            break;
                        } //报上级党委备案

                        //政治审查开展集中培训
                        if (partyMember.getNodeStatus().equals("3")) {
                            partyMember.setAttachmentIdDevelop(partyMember.getAttachmentIdDevelop() + "," + attchId1 + "," + attchId2 + "," + attchId3 + "," + attchId4 + "," + attchId5);
                            partyMember.setAttachmentNameDevelop(partyMember.getAttachmentNameDevelop() + "," + attachmentName1 + "," + attachmentName2 + "," + attachmentName3 + "," + attachmentName4 + "," + attachmentName5);
                            //提交下一阶段
                            partyMember.setState("2");
                            partyMember.setDevelopPhase("4");
                            partyMember.setNodeStatus("1");
                            partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                            break;
                        }

                        break;
                    case "4"://预备党员未接收
                        //case 4 阶段分为2步,1是没有接受时间,附件上传分别为5和6
                        if (partyMember.getNodeStatus().equals("0") || partyMember.getNodeStatus().equals("1")) {//刚到case4是没有附件的
                            partyMember.setAttachmentIdReceive(attchId1 + ","  + attchId3 + "," + attchId4 + "," + attchId5 + "," + attchId6 + "," + attchId7);
                            partyMember.setAttachmentNameReceive(attachmentName1 + ","  + attachmentName3 + "," + attachmentName4 + "," + attachmentName5 + "," + attachmentName6 + "," + attachmentName7);
                            partyMember.setState("2");//给前端释放权限 允许下一步工作进行
                            partyMember.setNodeStatus("3");
                            partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                            break;
                        }

                        //第2步是接受预备党员时间,附件上传
                        if (partyMember.getNodeStatus().equals("3")) {
                            partyMember.setAttachmentIdReceive(partyMember.getAttachmentIdReceive() + "," + attchId1 + "," + attchId2 + "," + attchId3 + "," + attchId4 + "," + attchId5 + "," + attchId6 + "," + attchId7);
                            partyMember.setAttachmentNameReceive(partyMember.getAttachmentNameReceive() + "," + attachmentName1 + "," + attachmentName2 + "," + attachmentName3 + "," + attachmentName4 + "," + attachmentName5 + "," + attachmentName6 + "," + attachmentName7);
                            partyMember.setReceiveTime(partyMemberWithBLOBs.getReceiveTime());//接收预备党员时间
                            partyMember.setState("2");//释放提交权限
                            partyMember.setNodeStatus("1");
                            partyMember.setDevelopPhase("5");//提交到下一个阶段
                            partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                            break;
                        }
                        break;
                    case "5"://已接收预备党员
                        //入党宣誓
                        if (partyMember.getNodeStatus().equals("0") || partyMember.getNodeStatus().equals("1")) {
                            partyMember.setPrepareTime(partyMemberWithBLOBs.getPrepareTime());//这里改为入党宣誓时间
                            partyMember.setAttachmentIdPrepare(attchId1);
                            partyMember.setAttachmentNamePrepare(attachmentName1);
                            partyMember.setState("2");
                            partyMember.setNodeStatus("2");
                            partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                            smsService.updatequerySmsByType("82",partyMember.getUserId(),"/partyMember/partyTR");
                            SmsBody smsBody = new SmsBody();
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            Date date = new Date();
                            smsBody.setFromId("admin");
                            smsBody.setContent("请提交思想汇报");
                            smsBody.setSendTime((int) (date.getTime() / 1000));
                            smsBody.setSmsType("82");
                            smsBody.setRemindUrl("/partyMember/partyTR");
                            String sqlType =tri.getSqlType();
                            //String sqlType = (String) request.getSession().getAttribute("loginDateSouse");
                            smsService.saveSms(smsBody, partyMember.getUserId(), "1", "2", "请提交思想汇报！", "请提交思想汇报", sqlType);
                            break;
                        }//四次思想汇报
/*
                            if (partyMember.getNodeStatus().equals("2")) {//第一次提交思想汇报
                                partyMember.setAttachmentIdPrepare(partyMember.getAttachmentIdPrepare() + "," + partyMemberWithBLOBs.getAttachmentId());
                                partyMember.setAttachmentNamePrepare(partyMember.getAttachmentNamePrepare() + "," + partyMemberWithBLOBs.getAttachmentName());
                                partyMember.setReports("5");
                                partyMember.setState("2");
                                partyMember.setNodeStatus("3");
                                partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                                break;
                            }
                            if (partyMember.getNodeStatus().equals("3")) {//第二次提交思想汇报
                                partyMember.setAttachmentIdPrepare(partyMember.getAttachmentIdPrepare() + "," + partyMemberWithBLOBs.getAttachmentId());
                                partyMember.setAttachmentNamePrepare(partyMember.getAttachmentNamePrepare() + "," + partyMemberWithBLOBs.getAttachmentName());
                                partyMember.setReports("6");
                                partyMember.setState("2");
                                partyMember.setNodeStatus("4");
                                partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                                break;
                            }
                            if (partyMember.getNodeStatus().equals("4")) {//第三次提交思想汇报
                                partyMember.setAttachmentIdPrepare(partyMember.getAttachmentIdPrepare() + "," + partyMemberWithBLOBs.getAttachmentId());
                                partyMember.setAttachmentNamePrepare(partyMember.getAttachmentNamePrepare() + "," + partyMemberWithBLOBs.getAttachmentName());
                                partyMember.setReports("7");
                                partyMember.setState("2");
                                partyMember.setNodeStatus("5");
                                partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                                break;
                            }
                            if (partyMember.getNodeStatus().equals("5")) {//第四次提交思想汇报后转变状态到发展对象
                                partyMember.setReports("8");//所有思想汇报全都完成
                                partyMember.setAttachmentIdPrepare(partyMember.getAttachmentIdPrepare() + "," + partyMemberWithBLOBs.getAttachmentId());
                                partyMember.setAttachmentNamePrepare(partyMember.getAttachmentNamePrepare() + "," + partyMemberWithBLOBs.getAttachmentName());
                                partyMember.setNodeStatus("6");
                                break;
                            }*/

                        if (partyMember.getNodeStatus().equals("2")) {
                            partyMember.setAttachmentIdPrepare(partyMember.getAttachmentIdPrepare() + "," + attchId1);
                            partyMember.setAttachmentNamePrepare(partyMember.getAttachmentNamePrepare() + "," + attachmentName1);
                            partyMember.setState("2");
                            partyMember.setNodeStatus("7");
                            partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                            break;
                        }
                        if (partyMember.getNodeStatus().equals("7")) {
                            partyMember.setAttachmentIdPrepare(partyMember.getAttachmentIdPrepare() + "," + attchIdAll + "," + partyMemberWithBLOBs.getAttachmentId());
                            partyMember.setAttachmentNamePrepare(partyMember.getAttachmentNamePrepare() + "," + attchmentNameAll + "," + partyMemberWithBLOBs.getAttachmentName());
                            partyMember.setDevelopPhase("6");
                            partyMember.setState("2");
                            partyMember.setNodeStatus("1");
                            partyMember.setStateTime(partyMemberWithBLOBs.getStateTime());
                            partyMember.setOldRunId(partyMember.getOldRunId() + "||" + partyMember.getRunId());
                            smsService.updatequerySmsByType("82",partyMember.getUserId(),"/partyMember/partyTR");
                            break;
                        }

                        break;


                    case "6"://党员
                        break;
                }
                partyMemberMapper.updateByPrimaryKeySelective(partyMember);
            }
        }
    }
}
